package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.FeedbackDto;
import io.geekidea.boot.shop.entity.Feedback;
import io.geekidea.boot.shop.mapper.FeedbackMapper;
import io.geekidea.boot.shop.query.app.AppFeedbackQuery;
import io.geekidea.boot.shop.query.web.FeedbackQuery;
import io.geekidea.boot.shop.service.base.api.FeedbackService;
import io.geekidea.boot.shop.vo.app.AppFeedbackVo;
import io.geekidea.boot.shop.vo.web.FeedbackVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addFeedback(FeedbackDto dto) {
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(dto, feedback);
        return save(feedback);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateFeedback(FeedbackDto dto) {
        Integer id = dto.getId();
        Feedback feedback = getById(id);
        if (feedback == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, feedback);
        return updateById(feedback);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteFeedback(Integer id) {
        return removeById(id);
    }

    @Override
    public FeedbackVo getFeedbackById(Integer id) {
        return feedbackMapper.getFeedbackById(id);
    }

    @Override
    public Paging<FeedbackVo> getFeedbackPage(FeedbackQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<FeedbackVo> list = feedbackMapper.getFeedbackPage(query);
        Paging<FeedbackVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppFeedbackVo getAppFeedbackById(Integer id) {
        return feedbackMapper.getAppFeedbackById(id);
    }

    @Override
    public Paging<AppFeedbackVo> getAppFeedbackPage(AppFeedbackQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppFeedbackVo> list = feedbackMapper.getAppFeedbackPage(query);
        Paging<AppFeedbackVo> paging = new Paging<>(list);
        return paging;
    }

}
